#include "stdafx.h"
#include "wf_selectwindow.h"

#include "lx_define.h"
#include "pf_snapper.h"
#include "pf_selection.h"
#include "pf_overlaybox.h"


/**
 * Constructor.
 *
 * @param select true: select window. false: deselect window
 */
WF_SelectWindow::WF_SelectWindow(PF_Container& container,
        PF_GraphicView& graphicView,
        bool select)
        : PF_PreviewActionInterface("Select Window",
                            container, graphicView) {

    this->select = select;
}


QAction* WF_SelectWindow::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {
    QAction* action;

    if (type==LX_Define::ActionSelectWindow) {
        action = new QAction(tr("Select Window"),  NULL);
		action->setIcon(QIcon(":/extui/selectwindow.png"));
        //action->zetStatusTip(tr("Selects all Entities in a given Window"));
    } else {
        action = new QAction(tr("Deselect Window"), NULL);
		action->setIcon(QIcon(":/extui/deselectwindow.png"));
        //action->zetStatusTip(tr("Deselects all Entities in a given Window"));		
	}
    return action;
}


void WF_SelectWindow::init(int status) {
    PF_PreviewActionInterface::init(status);
    v1 = v2 = PF_Vector(false);
    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_SelectWindow::trigger() {
    PF_PreviewActionInterface::trigger();

    if (v1.valid && v2.valid) {
        if (graphicView->toGuiDX(v1.distanceTo(v2))>10) {

            bool cross = (v2.y>v1.y);

            PF_Selection s(*container, graphicView);
            s.selectWindow(v1, v2, select, cross);

            PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());

            init();
        }
    }
}



void WF_SelectWindow::mouseMoveEvent(PF_MouseEvent* e) {
    if (getStatus()==SetCorner2 && v1.valid) {
        v2 = snapPoint(e);
        deletePreview();
		PF_Pen pen_f(PF_Color(50,50,255,40), LX_Define::Width00, LX_Define::SolidLine);
		PF_OverlayBox* ob=new PF_OverlayBox(preview, PF_OverlayBoxAttribute(v1, v2));
		ob->setPen(pen_f);	
		preview->addEntity(ob);
		
		PF_Pen pen(PF_Color(218,105,24), LX_Define::Width00, LX_Define::SolidLine);
		
		// TODO change to a PF_box sort of entity
		PF_Line* e=new PF_Line(preview, PF_LineAttribute(PF_Vector(v1.x, v1.y),  PF_Vector(v2.x, v1.y)));
		e->setPen(pen);
        preview->addEntity(e);

		e=new PF_Line(preview, PF_LineAttribute(PF_Vector(v2.x, v1.y),  PF_Vector(v2.x, v2.y)));
		e->setPen(pen);
        preview->addEntity(e);

		e=new PF_Line(preview, PF_LineAttribute(PF_Vector(v2.x, v2.y),  PF_Vector(v1.x, v2.y)));
		e->setPen(pen);
        preview->addEntity(e);

		e=new PF_Line(preview, PF_LineAttribute(PF_Vector(v1.x, v2.y),  PF_Vector(v1.x, v1.y)));
		e->setPen(pen);
        preview->addEntity(e);
		 
        drawPreview();
    }
}



void WF_SelectWindow::mousePressEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case SetCorner1:
            v1 = snapPoint(e);
            setStatus(SetCorner2);
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionSelectWindow::mousePressEvent(): %f %f",
                    v1.x, v1.y);
}



void WF_SelectWindow::mouseReleaseEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionSelectWindow::mouseReleaseEvent()");

    if (e->button()==Qt::LeftButton) {
        if (getStatus()==SetCorner2) {
            v2 = snapPoint(e);
            trigger();
        }
    } else if (e->button()==Qt::RightButton) {
        if (getStatus()==SetCorner2) {
            deletePreview();
        }
        init(getStatus()-1);
    }
}



void WF_SelectWindow::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetCorner1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Choose first edge"), tr("Cancel"));
        break;
    case SetCorner2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Choose second edge"), tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_SelectWindow::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::SelectCursor);
}



void WF_SelectWindow::updateToolBar() {
    if (!isFinished()) {
        //PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSelect);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSelect);
    }
}

// EOF
